package ArrayList1;

import java.util.*;

public class ArrayList {
    public static void main(String[] args) {
//       ArrayList1();

//        ArrayList arrayList = new ArrayList();
//        arrayList.Arraylist2();
//        arrayList.randList();
//        map = new HashMap<>();
//        map.put("1", "aa");
//        map.put("2", "dd");
//        map.put("3", "cc");
//        map.put("4", "bb");
//        keySetMethod();
//        HashMap<String,String>map=new HashMap<>();
//        map.put("1", "aa");
//        map.put("2", "dd");
//        map.put("3", "cc");
//        map.put("4", "bb");
//        KeySetMethod1(map);
        entrySetMethod();
    }

    public static void ArrayList1() {
        int total = 10;
        String[] strings = new String[total];
        for (int i = 0; i < strings.length; i++) {
            strings[i] = String.valueOf(i);

        }
        List<String> list = Arrays.asList(strings);//数组转换为集合
        for (String s : list) {
            System.out.println("s = " + s);
        }
    }

    public void Arraylist2() {
        String[] str = {"java", "php", "web", "c++"};
        List<String> list = Arrays.asList(str);
        //反转+迭代器
        ListIterator<String> stringListIterator = list.listIterator();
        Collections.sort(list);
        System.out.println("反转前");
        while (stringListIterator.hasNext()) {
            System.out.println("stringListIterator.next() = " + stringListIterator.next());
        }
        Collections.reverse(list);
        System.out.println("翻转后");
        ListIterator<String> stringListIterator1 = list.listIterator();
        while (stringListIterator1.hasNext()) {
            System.out.println("stringListIterator1.next() = " + stringListIterator1.next());
        }
    }
//打乱集合顺序输出
    public void randList() {
        java.util.ArrayList<Integer> integers = new java.util.ArrayList<>();
        for (int i = 0; i < 10; i++) {
            integers.add(i);
        }
        System.out.println("打乱前");
        System.out.println("integers = " + integers);
        for (int i = 0; i < 5; i++) {
            System.out.println("第" + i + "次打乱");
            Collections.shuffle(integers);
            System.out.println("integers = " + integers);

        }
    }
//定义全局变量
    public static Map<String, String> map;
//全局遍历
    public static void keySetMethod() {
        Set<String> strings = map.keySet();
        Iterator<String> iterator = strings.iterator();
        while (iterator.hasNext()) {
            System.out.println("iterator.next() = " + iterator.next());
        }
    }
    //局部遍历
    public static void KeySetMethod1(HashMap<String,String>map){
        Set<String>strings1=map.keySet();
        Iterator<String> iterator = strings1.iterator();
        while (iterator.hasNext()){
            System.out.println("iterator.next() = " + iterator.next());
        }
    }
    //键值对遍历
    private static void entrySetMethod(){
        map=new HashMap<>();
        map.put("1", "aa");
        map.put("2", "dd");
        map.put("3", "cc");
        map.put("4", "bb");
        Set<Map.Entry<String, String>> entries = map.entrySet();
        Iterator<Map.Entry<String, String>> iterator = entries.iterator();
        while (iterator.hasNext()){
            Map.Entry<String, String> next = iterator.next();
            System.out.println("next.getKey() = " + next.getKey());
            System.out.println("next.getValue() = " + next.getValue());
            System.out.println("-----------------");
        }
    }
}
